package com.pi.pro.xcmg.am.was.web.cust.controller;

import com.alibaba.fastjson.JSONArray;
import com.pi.common.CodesConstants;
import com.pi.common.utils.StringUtils;
import com.pi.framework.core.service.ICrudService;
import com.pi.framework.core.util.DateUtils;
import com.pi.framework.core.util.SecurityUtils;
import com.pi.framework.core.util.csv.CsvExporter;
import com.pi.framework.web.util.CsvView;
import com.pi.pro.xcmg.am.common.XcmgAmConstants;
import com.pi.wms.entity.TwaInventoryDetails;
import lombok.extern.slf4j.Slf4j;
import org.springframework.data.jpa.datatables.mapping.DataTablesInput;
import org.springframework.stereotype.Controller;
import org.springframework.ui.Model;
import org.springframework.web.bind.annotation.*;
import org.springframework.web.servlet.ModelAndView;
import org.springframework.web.servlet.mvc.support.RedirectAttributes;

import javax.servlet.http.HttpServletRequest;
import java.util.*;


@Slf4j
@Controller
@RequestMapping("/inventory/otinv")
public class OtInventoryController extends BaseInventoryController<TwaInventoryDetails, Long> {

	@Override
	protected ICrudService<TwaInventoryDetails, Long> getCrudService() {
	    return service;
	}

	@Override
	protected String getPrefix() {
		return "otinv";
	}

	@Override
	protected TwaInventoryDetails newModel(HttpServletRequest request) {
		return new TwaInventoryDetails();
	}

	@Override
	protected void prepareDatatableConditions(Map<String, Object> query, DataTablesInput input) {
		Long userId = SecurityUtils.getLoginUserId();
		query.put("userId", userId.toString());
		query.put("reservecode", XcmgAmConstants.WORK_SOURCE_OTHER);
		super.prepareDatatableConditions(query, input);
	}

    @Override
    @PostMapping({"/export"})
    public ModelAndView export(@RequestParam final Map<String, Object> query, DataTablesInput input) {
        query.put("reservecode", XcmgAmConstants.WORK_SOURCE_OTHER);
        String inputParams = "";
        if(query.get("input") != null) {
            inputParams = query.get("input").toString();
        }
        if(StringUtils.isNotEmpty(inputParams)) {
            input = (DataTablesInput) JSONArray.parseObject(inputParams, DataTablesInput.class);
        }
        input.setLength(Integer.valueOf(-1));
        String ymd = DateUtils.format(new Date(), "yyyyMMddHHmmss");
        final DataTablesInput ins = input;
        CsvExporter<TwaInventoryDetails> exporter =  this.createExporter(query, ins, this.getMaterials(), this.getLotattrs());
        return new ModelAndView(new CsvView(exporter, "inventory_list_" + ymd));
    }


    @Override
    @GetMapping("del/{id}")
    public String del(@PathVariable("id") Long id, Model model, HttpServletRequest request, RedirectAttributes redirectAttributes) {
        model.addAttribute("params", getParams(request));
        String m="";
        if (id != null) {
            m=this.delete(id);
            if (m==null){
                m="发生异常,请联系系统管理员";
            }
            if (CodesConstants.COMMON_STATUS_SUCCESS.equalsIgnoreCase(m)){
                m="";
            }
            if(!com.pi.common.utils.StringUtils.isNullOrBlank(m)){
                addErrorMessage(redirectAttributes, "删除失败:"+m);
                return this.redirect("");
            }
            addSuccessMessage(redirectAttributes, "删除数据成功");
        }
        return this.redirect("");
    }


    @Override
    @PostMapping("del")
    public String del(@RequestParam("ids") Long[] ids, Model model, HttpServletRequest request, RedirectAttributes redirectAttributes) {
        model.addAttribute("params", getParams(request));
        String m="";
        if (ids != null && ids.length > 0) {
            for (Long id:ids){
                m=this.delete(id);
                if (m==null){
                    m="发生异常,请联系系统管理员";
                }
                if (CodesConstants.COMMON_STATUS_SUCCESS.equalsIgnoreCase(m)){
                    m="";
                }
                if(!com.pi.common.utils.StringUtils.isNullOrBlank(m)){
                    addErrorMessage(redirectAttributes, "删除失败:"+m);
                    return this.redirect("");
                }
                //addSuccessMessage(redirectAttributes, "删除成功");
            }
            addSuccessMessage(redirectAttributes, "删除数据成功");
        }
        return this.redirect("");
    }


	
}
